#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Dependecis:
PyGTK 2.10=<
pyobject 2.10=<
sqlite3
universal feed parser
"""

import sys,time
import md5
import os,os.path
import threading
import feedparser
import thread,re
import pango
import gtkhtml2
from pysqlite2 import dbapi2 as sqlite
try:
     import pygtk
     pygtk.require("2.0")
except:
      pass
try:
    import gtk,gobject
    import gtk.glade
except:
    sys.exit(1)
gobject.threads_init()
import urllib
import urlparse
import dbus
import dbus.service 
if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
    import dbus.glib
    
# XPM
WheelbarrowFull_xpm = [
"200 130 3 1",
"     c None",
".    c #000000",
"+    c #FFFFFF",
"                           ..                                                                                                                                                                           ",
"                          ....                                                                                                                                                                          ",
"                         ......                                                                                                                                                                         ",
"                        ........                                                                                                                                                                        ",
"                       ..........                                                                                                                                                                       ",
"                      ............                                                                                                                                                                      ",
"                      ....++......                                                                                                                                                                      ",
"                      ....+++......                                                                                                                                                                     ",
"                      ....+++......                                                                                                                                                                     ",
"                      ....++++.....                                                                                                                                                                     ",
"                     ....+++++......                                                                                                                                                                    ",
"                     ....+++++.......                                                   ................................                                                                                ",
"                     ...++++++.......                                            ..............................................                                                                         ",
"                     ...+++++++.......                                     ..........................................................                                                                   ",
"                    ...++++++++.......                                 ..................................................................                                                               ",
"                    ...+++++++++......                             ..........................................................................                                                           ",
"                    ...+++++++++......                          ............................++++++++............................................                                                        ",
"                   ....++++++++++......                      .........................++++++++++++++++++++++++++++++++.............................                                                     ",
"                  .....++++++++++......                   .......................+++++++++++++++++++++++++++++++++++++++++++..........................                                                  ",
"                  ....++++++++++++......                .....................+++++++++++++++++++++++++++++++++++++++++++++++++++........................                                                ",
"                  ....++++++++++++......             ......................+++++++++++++++++++++++++++++++++++++++++++++++++++++++++.......................                                             ",
"                  ....+++++++++++++......          ..................+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.....................                                           ",
"                  ....+++++++++++++......        ................+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.....................                                         ",
"                  ....++++++++++++++.....      ..............+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.....................                                       ",
"                 .....++++++++++++++.....    ............++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++......................                                     ",
"                 .....+++++++++++++++..... ............+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.......................                                   ",
"                 .....++++++++++++++++...............+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.....................                                 ",
"                 .....+++++++++++++++++.........+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.....................                                ",
"                 .....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.....................                              ",
"                ......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++....................                             ",
"                ......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++....................                           ",
"                ......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...................                          ",
"                ......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++..................                         ",
"                ......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++..................                       ",
"                ......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.................                      ",
"                ......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++..............                     ",
"                ......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.............                    ",
"               .......++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.............                   ",
"               .......++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++............                  ",
"               ......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...........                 ",
"               ......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++..........                ",
"               ......++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++..........               ",
"               ......++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.........              ",
"               ......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++........              ",
"               .....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++........             ",
"               .....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++........            ",
"               .....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++........           ",
"               .....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.......           ",
"               .....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++......          ",
"               .....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.....          ",
"               ....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++..         ",
"                ...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++..        ",
"                ...++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.        ",
"                ...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++        ",
"               ....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.       ",
"               ....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.       ",
"              ....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++..      ",
"              ....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++..      ",
"              ....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++..      ",
"             .....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...     ",
"             .....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...     ",
"             .....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...     ",
"            ......++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++....    ",
"            ......++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++....    ",
"            ......++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++....    ",
"            ......++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++....    ",
"            ......++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++....    ",
"            .....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++....    ",
"            .....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++....    ",
"            .....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...    ",
"            .....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...    ",
"            .....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...    ",
"            .....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...    ",
"            .....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...    ",
"            .....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...    ",
"            .....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...    ",
"            .....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...    ",
"            .....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...    ",
"            .....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...    ",
"            .....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...    ",
"             ....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++..     ",
"             ....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...     ",
"             .....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...     ",
"              ....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...      ",
"              ....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...      ",
"              ....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++....      ",
"               ...++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...       ",
"               ....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++....       ",
"                ...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++....        ",
"                ...++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.....        ",
"                ...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++......        ",
"                 ...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++......         ",
"                  ..++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++......          ",
"                  ..+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.......          ",
"                   ..++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++......           ",
"                   ...++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.......           ",
"                    ...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++......            ",
"                     ...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++......             ",
"                      ...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++......              ",
"                      ....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.......              ",
"                       ....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.......               ",
"                        .....++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.......                ",
"                         ......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.......                 ",
"                          ......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.......                  ",
"                           ......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.......                   ",
"                            .......++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.......                    ",
"                             .......++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.......                     ",
"                              .......++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.......                      ",
"                               .........+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++........                       ",
"                                 ..........++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++........                         ",
"                                  ...........++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.........                          ",
"                                   ...........++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.........                           ",
"                                     ...........+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++........                             ",
"                                      ...........+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++........                              ",
"                                        ..........++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++........                                ",
"                                         .............++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.........                                 ",
"                                           ..............+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...........                                   ",
"                                             ................+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.............                                     ",
"                                               .....................++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.............                                       ",
"                                                 ...................++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...............                                         ",
"                                                   ...................++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...............                                           ",
"                                                     .....................++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...............                                             ",
"                                                        ......................++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++..............                                                ",
"                                                          ..........................++++++++++++++++++++++++++++++++++++++++++++++++++++..............                                                  ",
"                                                             ............................+++++++++++++++++++++++++++++++++++++++++++++.............                                                     ",
"                                                                ...............................+++++++++++++++++++++++++++......................                                                        ",
"                                                                   .......................................+++++++++++++++....................                                                           ",
"                                                                       .............................................++++.................                                                               ",
"                                                                           ..........................................................                                                                   ",
"                                                                                 ..............................................                                                                         ",
"                                                                                        ................................                                                                                ",
"                                                                                                                                                                                                        ",
"                                                                                                                                                                                                        "]


opener = urllib.FancyURLopener()
currentUrl = None

def is_relative_to_server(url):
    parts = urlparse.urlparse(url)
    if parts[0] or parts[1]:
        return 0
    return 1

def open_url(url):
    uri = resolve_uri(url)
    return opener.open(uri)

def resolve_uri(uri):
    if is_relative_to_server(uri):
        return urlparse.urljoin(currentUrl, uri)
    return uri
 
def request_url(document, url, stream):
    
    thread = http_link(url,stream)
    thread.start();
    stream.set_cancel_func(thread.cancel)
     
    #f = open_url(url)
    #stream.write(f.read())
 
def is_hebrew(string):
    letters = [u"א",u"ב",u"ג",u"ד",u"ה",u"ו",u"ז",u"ח",u"ט",u"י",u"כ",u"ל",u"מ",u"נ",u"ס",u"ע",u"פ",u"צ",u"ק",u"ר",u"ש",u"ת"]
    """found = 0; 
    for letter in letters:
        if(letter==string):
            found = 1;"""
    check = re.compile("[א-ת]",re.M).search(string, 1)
    check2 = re.compile(u"[א-ת]",re.M).search(string, 1)
    if check != None or check2 != None:
        found = 1;
    else: found = 0;  
    return found;
    """found =0
    for letter in string or found==1:
        if letter in letters:
            found =1;
     
    return found;"""

class pyReadr:
    """This is the PyReadr application"""
    wTree = None;
    popup_wTree = None;
    trayicon = None;
    lock = None; 
    displayed_post = None
    displayed_rss = None;
    balloon = None;
    hide = 0;
    threads = []
    showed_new_channels = [];
    def __init__(self): 
        #Set the Glade file
        self.gladefile = "pyreadr.glade"  
        self.wTree = gtk.glade.XML(self.gladefile, "main_window") 

        #Create our dictionay and connect it
        dic = { "test":self.quit, \
                   "on_main_window_frame_event" :self.close_window, \
                   #"on_main_window_destroy" : gtk.main_quit, \
                   "popup_channels_menu" : self.pop_up_channels, \
                   "open_add_rss": self.open_add_rss, \
                   "rss_changed": self.test,\
                   "display_post": self.display_post,\
                   "btn_mark_all_channels_as_read":self.mark_all_channels_posts_as_read,\
                   "open_proporties" : self.open_proporties,\
                   "refresh" : self.refresh_all
               }
        self.wTree.signal_autoconnect(dic)
        if(os.path.exists(os.path.expanduser("~/.pyreadr"))):
            if not os.path.exists(os.path.expanduser("~/.pyreadr/readr.db")) :
                self.new_db();
            else:
                con = sqlite.connect(os.path.expanduser("~/.pyreadr/readr.db"));
                con.row_factory = sqlite.Row 
                self.db = con;  
        else:
            os.mkdir(os.path.expanduser("~/.pyreadr"));
            self.new_db();
        self.populate_channels(None) 
        self.trayicon = gtk.status_icon_new_from_file('feed-icon.svg');
        #self.trayicon = gtk.status_icon_new_from_stock(gtk.STOCK_OK);
        self.trayicon.set_visible(1); 
        self.trayicon.set_tooltip("PyReadr - A simple RSS reader written in Python"); 
        self.trayicon.connect("activate",self.ty_activated)
        #self.trayicon.show_all();
        menu_wTree = gtk.glade.XML(self.gladefile,"statusicon_menu");
        menu_wTree.signal_autoconnect({"close" : self.quit, "refresh" : self.refresh_all,"show":self.ty_activated});
        menu = menu_wTree.get_widget("statusicon_menu")
        menu.show_all();
        ret = gtk.status_icon_position_menu(menu, self.trayicon)
        self.ty_menu = menu
        self.trayicon.connect("popup-menu",self.ty_popmenu)
        
        posts_tv = self.wTree.get_widget("posts_tv");
        model  = gtk.ListStore(gobject.TYPE_INT, gobject.TYPE_STRING,gobject.TYPE_INT);
        posts_tv.set_model(model)
        cr = gtk.CellRendererText()
        #posts_tv.append_column(gtk.TreeViewColumn("ID",gtk.CellRendererText(),text=0)) 
        #posts_tv.append_column(gtk.TreeViewColumn("ID",gtk.CellRendererText(),text=0)); 
        
        title_clumn = gtk.TreeViewColumn("Title",cr,markup=0,text=1);
        posts_tv.append_column(title_clumn); 
        #posts_tv.append_column(gtk.TreeViewColumn("read",gtk.CellRendererText(),text=2)); 
        title_clumn.set_cell_data_func(cr, self.cell_title_data)
        #vbox3
        document = gtkhtml2.Document()
        document.connect('link_clicked', self.link_clicked)
        document.connect('request_url', request_url)
        
        
        document.clear()
        #document.open_stream('text/html')
        #document.write_stream(u'<html bgcolor="white"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta name="qrichtext" content="1" /></head><body style="font-size:10pt;font-family:Bitstream Vera Sans"><p dir="rtl"><img align=left alt="SELinux Trouble Shooting Tool" height=74 hspace=15 src=http://static.flickr.com/90/264766236_9522758af4_m.jpg title="SELinux Trouble Shooting Tool" vspace=15 width=240 >מועד שיחרורה של Fedora Core 6 <a href="https://www.redhat.com/archives/fedora-announce-list/2006-October/msg00000.html">נדחה</a> בשבוע (<a href="http://fedoraproject.org/wiki/Core/Schedule">לו&quot;ז</a>), מה-11 באוקטובר ל-17, לאחר שהתגלו מספר באגים קריטיים, בהם השחתה אפשרית של מערכת הקבצים Ext3 וקפיאה במהלך ההתקנה במחשבים שמצויידים ב-256 מ&quot;ב זיכרון RAM ואחרים. אבל יש גם בשורות טובות: FC6 תשלב מספר שיפורי שמישות וביצועים מסקרנים.</p></body></html>');
        #document.close_stream()
        view = gtkhtml2.View()
        view.connect("on-url", self.link_hover) 
        view.set_document(document)
        sw = gtk.ScrolledWindow()
        #sw.add(view)
        sw.show_all();
        self.html = document;
        
        self.wTree.get_widget("scorrled_window").add(view);
        self.wTree.get_widget("scorrled_window").show_all()
        
        self.progressbar = gtk.ProgressBar();
        self.wTree.get_widget("statusbar").pack_end(self.progressbar,0,0,0);
        
        self.progressbar.show()
    def close_window(self,widget,event):
        """nothing"""
        #print "close";
        #self.hide=1;
        self.position = self.wTree.get_widget("main_window").get_position()

        if event.type == gtk.gdk.DELETE:
            self.hide=1;
            self.wTree.get_widget("main_window").hide()
            #self.wTree.get_widget("main_window").visible = False;
            return True;
    def open_proporties(self,widget):
        dialog = options_dialog(self.gladefile,self.db);
        dialog.run();
        #menu_wTree.signal_autoconnect({"close" : self.quit, "refresh" : self.refresh_all,"show":self.ty_activated});

    def ty_popmenu(self,status_icon, button, activate_time):
 
        self.ty_menu.popup(None, None, None, button, activate_time)
    def add_rss_dialog(self,feedurl):
        """opens the add dialog with a specific url"""
        dialog = AddRss(self.gladefile,self.db);
        dialog.run(self.populate_channels,feedurl); 
    def refresh_all(self,widget=None):
        channels = self.db.cursor(); 
        channels.execute("select * from channels"); 
        
        for row in channels:  
            th_new = rss_thread(self.db,row[2],row[0]);
            th_new.start();
            self.threads.append(th_new);
        channels.close()   
        return True
    def link_hover(self,view,link):
        if not link==None:
            self.wTree.get_widget("statusbar").push(self.wTree.get_widget("statusbar").get_context_id("link"),link);
        else:
            self.wTree.get_widget("statusbar").pop(self.wTree.get_widget("statusbar").get_context_id("link"))
    def ty_activated(self,user_param):
        if self.hide == 0:
            self.wTree.get_widget("main_window").hide() 
            self.hide = 1;
        else:
            print self.position
            self.wTree.get_widget("main_window").move(self.position[0],self.position[1])

            self.wTree.get_widget("main_window").show_all()
            self.hide = 0;
    def link_clicked(self,document, link):
        cmd = "gnome-open \""+link+"\"&"
        #pid = os.fork()
        pid = 0;
        if(pid==0):
            print cmd;
            os.system(cmd);
            #sys.exit(sys.EX_OK)._exit()
        else:
            print "opened"
    def display_post(self,widget):
        liststore,iter = widget.get_selection().get_selected();
        if iter != None:
            id = liststore.get_value(iter,0);
            if id!=self.displayed_post: 
               self.displayed_post = id;
               self.display_actual_post(id);
    def check_w_mark_post_read(self,id):
        if self.displayed_post == id:
            mark = self.db.cursor();
            mark.execute("UPDATE posts SET read=1 WHERE id="+id.__str__());
            mark.close();
            self.db.commit() 
            self.refresh_gui_treeviews()
    def display_actual_post(self,id):
        post = self.db.cursor();
        post.execute("select * from posts WHERE id="+str(id)+" LIMIT 1");
        document = self.html
        for row in post:  
            #print row['title'],row['text']
            document.clear()
            self.displayed_post = id;
            document.open_stream('text/html') 
            if is_hebrew(row['title']):
                dir= "rtl";
            else:
                dir="ltr"
                
            if is_hebrew(row['text']):
                align_text = "right";
            else:
                align_text = "left"; 
            document.write_stream(
u"""
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="qrichtext" content="1" />
</head>
<body style="font-size:10pt;margin:0px;margin-right:2px;font-family:Bitstream Vera Sans">
<div dir='"""+dir+"""' style='color:white;background:#5598d7;border:1px solid #a39c8b;padding: 5px' align="center">
<b>"""+row['title']+"""<b>
</div>
<div style="height:5px"></div>
<div style="background: white;border:1px solid #a39c8b;">
<table bgcolor="white" style='background: white;padding: 5px'>
<tr>

<td align='"""+align_text+"""' dir='"""+dir+"""'>
"""+row['text']+"""
</td>
</tr>
<tr>
<td>
<hr>
</td>
</tr>
<tr>
<td>
<a href="""""+row['link']+""">"""+row['link']+"""</a> | """+row['updated']+"""
</td>
</tr>
</table>
</div>
</body>
</html>"""
            );
            document.close_stream()
            #print [row['id'],row['title'],row['read']];
        post.close()
        if row['read']==0:
            gobject.timeout_add(1000, readr.check_w_mark_post_read,id)
    def new_db(self):
        """opens new sqlite db in ~/.pyteadr/readr.db"""
        con = sqlite.connect(os.path.expanduser("~/.pyreadr/readr.db"));
        con.row_factory = sqlite.Row 
        cur = con.cursor()
        cur.execute("CREATE TABLE channels (id INTEGER PRIMARY KEY, name TEXT, url TEXT);")  
        cur.execute("CREATE TABLE posts (read NUMERIC, text , link TEXT, updated TEXT, title TEXT, channel_id TEXT, updated_hash TEXT, id INTEGER PRIMARY KEY);")  
        cur.execute("CREATE UNIQUE INDEX id ON channels(id ASC);")      
        cur.close();
        self.db = con;       
        
    def open_add_rss(self, widget=None):
        """open add_rss dialog"""
        dialog = AddRss(self.gladefile,self.db);
        dialog.run(self.populate_channels); 
        #dialog.connect("destroy",self.populate_channels) 
    def pop_up_channels(self,a,b):
        """popup the channel menu"""
        if b.button == 3:#
            if self.popup_wTree == None:
                self.popup_wTree = gtk.glade.XML(self.gladefile,"channels_popup");
                signals = {"refresh_channel": self.refresh_channel, "delete_channel":self.delete_channel, "mark_all_posts_as_read": self.mark_all_posts_as_read}
                self.popup_wTree.signal_autoconnect(signals)
            popup = self.popup_wTree.get_widget("channels_popup");
            popup.popup(None,None,None,b.button,b.time);

    def __del__(self):
        self.db.close(); 
    def refresh_gui_treeviews(self):
        """posts_tv = self.wTree.get_widget("posts_tv");
            post_path,post_focus = posts_tv.get_cursor()
            tv = self.wTree.get_widget("channels");
            path,focus = tv.get_cursor()
           
            n_rows = len(tv.get_model())
            if path == None:
                posts_tv.get_model().clear();
            else:
                #tv.get_selection().set_mode(gtk.SELECTION_NONE);
                #tv.get_selection().set_mode(gtk.SELECTION_SINGLE)
                #tv.set_cursor((0,));
                if not n_rows==0:
                    if n_rows-1 == path[0] and n_rows-1>0:
                        tv.set_cursor((path[0]-1,));
                    else:
                        tv.set_cursor((path[0]+1,));
                    tv.set_cursor((path[0],));

                else:
                    liststore,iter = self.wTree.get_widget("channels").get_selection().get_selected();
                    if iter != None:
                        id = liststore.get_value(iter,0);
                    self.display_rss(id) 
                if post_path==None:
                    posts_tv.set_cursor(post_path);
                else:
                    print "a";
                    posts_tv.set_cursor((post_path[0]+1,));
                    posts_tv.set_cursor((post_path[0],));
                """
        posts_tv = self.wTree.get_widget("posts_tv");
        post_path,post_focus = posts_tv.get_cursor()
        channels_tv = self.wTree.get_widget("channels");
        path,focus = channels_tv.get_cursor();
        liststore,post_iter = posts_tv.get_selection().get_selected()
        lsc, ch_iter = channels_tv.get_selection().get_selected()
        if ch_iter!=None:
            path = lsc.get_path(ch_iter);
        if post_iter!=None:
            post_path = liststore.get_path(post_iter);
        #post_id = liststore.get_value(iter,0);
        channel_id = self.get_selected_channel()
        if channel_id!=-1:
            self.display_rss(channel_id)
        #print path
        #if post_path!=None:
        if post_iter!=None:
            posts_tv.get_selection().select_path(post_path)
        
        self.populate_channels(None);
        #if path!=None:
        if ch_iter!=None:    
            channels_tv.get_selection().select_path(path)
    def get_selected_channel(self):
        channels_tv = self.wTree.get_widget("channels");
        liststore,iter = channels_tv.get_selection().get_selected()
        if iter != None:
            channel_id = liststore.get_value(iter,0);
            return channel_id
        else:
            return -1;
    def mark_all_posts_as_read(self,widget):
        tv = self.wTree.get_widget("channels"); 
        liststore,iter = tv.get_selection().get_selected()
        id = liststore.get_value(iter,0);
        mark = self.db.cursor();
        mark.execute("UPDATE posts SET read=1 WHERE channel_id="+id.__str__());
        mark.close();
        self.db.commit()
        self.refresh_gui_treeviews();
    def refresh_channel(self,widget): 
        tv = self.wTree.get_widget("channels"); 
        liststore,iter = tv.get_selection().get_selected()
        id = liststore.get_value(iter,0);
        link = liststore.get_value(iter,2); 
        th = rss_thread(self.db,link,id);#thread.start_new_thread(get_rss_channel,(None,));
        th.start() 
    def delete_channel(self,a):
        tv = self.wTree.get_widget("channels"); 
        liststore,iter = tv.get_selection().get_selected()
        if iter!=None:
            id = liststore.get_value(iter,0);
             
            con = self.db 
            cur = con.cursor()
            cur.execute("DELETE FROM channels WHERE id="+str(id)) 
            cur.execute("DELETE FROM posts WHERE channel_id="+str(id)) 
            
            #print ("INSERT INTO channels (name,url) VALUES('"+self.wTree.get_widget("rss_name").get_text()+"','"+self.wTree.get_widget("rss_url").get_text()+"');") 
            con.commit();
            cur.close();
            self.populate_channels(None);
        self.refresh_gui_treeviews();
        
    def quit(self, widget):
        """Called when the user want to exit""" 
        self.ty_activated("");
        for thread in self.threads:
            if thread.isAlive():
                thread.stop_th(); 
        for thread in self.threads:
            if thread.isAlive():      
                thread.join();
        gtk.main_quit();
    def cell_channel_title_data(self,column, cell, model, iter):
 
        cell.set_property('weight-set', 0)        
        if model.get_value(iter, 3)>0:
            cell.set_property('weight', pango.WEIGHT_ULTRABOLD)
            cell.set_property('weight-set', 1)
  
    def populate_channels(self,widget):
        """populate the channels treeview""" 
        
        tv = self.wTree.get_widget("channels"); 
        if not tv.get_model():
            model  = gtk.ListStore(gobject.TYPE_INT, gobject.TYPE_STRING, gobject.TYPE_STRING,gobject.TYPE_INT);
            tv.set_model(model)
            tv.append_column(gtk.TreeViewColumn("ID",gtk.CellRendererText(),text=0)) 
            cr = gtk.CellRendererText()
            channel_column = gtk.TreeViewColumn("Rss Channels",cr,text=1);
            tv.append_column(channel_column) 
            channel_column.set_cell_data_func(cr, self.cell_channel_title_data)
        else:
            model = tv.get_model();
            model.clear()    
        
        channels = self.db.cursor();
        channels.execute("select * from channels");
        for row in channels:
            #check if channel has unread post, if so add 1 to the last column in the modal
            check = self.db.cursor();
            check.execute("SELECT COUNT(*) FROM posts WHERE read=0 AND channel_id="+str(row[0]));
            for read in check:
                read_last = read[0];
            check.close();
            del check 
            model.append([row[0],row[1],row[2],read_last])
        channels.close()  
    def mark_all_channels_posts_as_read(self,w=None): 
        mark = self.db.cursor();
        mark.execute("UPDATE posts SET read=1");
        mark.close();
        self.refresh_gui_treeviews();
    def get_balloon_string(self):
        channels = self.db.cursor();
        channels.execute("select channel_id,COUNT(*) as unread from posts where read!=1 GROUP BY channel_id");
        str ="";
        chs = [];
        for row in channels:
            #print row[0],row[1];
            #str = str+row['title']+"\n";
            channel = self.db.cursor();
            channel.execute("SELECT * FROM channels WHERE id="+row['channel_id']);
            
            chs.append(row['channel_id']);
            for ch in channel:
                #print ch[1]
                
                if not row['channel_id'] in self.showed_new_channels:
                    str = str + ch[1] + ": "+row['unread'].__str__()+"\n"
                    self.showed_new_channels.append(row['channel_id'])
            channel.close
            
            del channel;
        channels.close()  
        del chs
        return str;
    def new_post_event_check(self): 
        #GDK_WINDOW_TYPE_HINT_TOOLTIP
        #gtk.gdk.WINDOW_TYPE_HINT_TOOLTIP
        str =     self.get_balloon_string();
        #for ch_id in chs:
         #   if not ch_id in self.showed_new_channels:
          #      self.get_balloon_string();
        if self.balloon == None:
            """window = gtk.Window(type=gtk.WINDOW_POPUP);
            label = gtk.Label("New RSS feeds:        \n");
            eventbox = gtk.EventBox()
            eventbox.set_events(gtk.gdk.BUTTON_PRESS_MASK)
            eventbox.connect("button-press-event",self.close_balloon)
    
            
            eventbox.add(label)
            window.add(eventbox);   
            #window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_MENU);
            ty= readr.trayicon
            a,rect,asd =ty.get_geometry(); 
            #print rect.x,abs(rect.y-ty.get_size());
            window.move(rect.x,abs(rect.y-(ty.get_size()+5)))#x,y
            #window.set_decorated(0);
            gobject.timeout_add(3000, self.close_balloon)#window.destroy)
            window.show_all();  """
            window = gtk.Window(gtk.WINDOW_POPUP)
            window.connect("delete_event", self.close_application)
            window.set_events(window.get_events() | gtk.gdk.BUTTON_PRESS_MASK)
            window.connect("button_press_event", self.close_application)
            window.show()
    
            # Now for the pixmap and the image widget
            pixmap, mask = gtk.gdk.pixmap_create_from_xpm_d(
                window.window, None, WheelbarrowFull_xpm)
            image = gtk.Image()
            image.set_from_pixmap(pixmap, mask)
            image.show()
    
            # To display the image, we use a fixed widget to place the image
            fixed = gtk.Fixed()
            fixed.set_size_request(200, 200)
            fixed.put(image, 0, 0)
            label = gtk.Label("New RSS feeds:\n");
            fixed.put(label, 39, 39)
            label.show();
            window.add(fixed)
            fixed.show()
    
            # This masks out everything except for the image itself
            window.shape_combine_mask(mask, 0, 0)
            window.show()
            ty= readr.trayicon
            a,rect,asd =ty.get_geometry(); 
            window.move(rect.x-(ty.get_size()/2),abs(rect.y-(ty.get_size()+5)))#x,y
            self.balloon = window;
            self.balloon_label = label;
        else:
            label = self.balloon_label;
        label.set_text(label.get_text()+str);
    def close_application(self, widget, event, data=None):
        self.close_balloon()
        return False
    def close_balloon(self,a=None,event=None):
        """close balloon on click""" 
        if self.balloon != None:
            self.balloon.destroy();
            self.balloon_label = None;
            self.balloon = None;
            self.showed_new_channels = [];
        #print self.balloon.visible;
    def test(self,a):
        liststore,iter = a.get_selection().get_selected();
        if iter != None:
            id = liststore.get_value(iter,0);
            if id!=self.displayed_rss: 
               self.displayed_rss = id;
               self.display_rss(id);
           
    def display_rss(self,id):
        model = self.wTree.get_widget("posts_tv").get_model();
        model.clear();
        cell = self.wTree.get_widget("posts_tv")
        posts = self.db.cursor();
        posts.execute("select * from posts WHERE channel_id="+str(id)+" ORDER BY id DESC ");
        is_rtl = 0;
        for row in posts:  
            if not is_rtl:
                is_rtl = is_hebrew(row['title']) or is_hebrew(row['text']) 
            model.append([row['id'],row['title'],row['read']])
            #print [row['id'],row['title'],row['read']];
        posts.close()
        if is_rtl:
            scwindow = self.wTree.get_widget("posts_scwindo");
            hj = scwindow.get_hadjustment();
            hj.value=hj.upper 
            scwindow.set_hadjustment(hj)
        else:
            scwindow = self.wTree.get_widget("posts_scwindo");
            hj = scwindow.get_hadjustment();
            hj.value=hj.lower
            scwindow.set_hadjustment(hj)
    def cell_title_data(self,column, cell, model, iter):
        
        """bold=pango.AttrList()
        bold.insert(pango.AttrWeight(pango.WEIGHT_BOLD,0,-1))
        column.set_attributes(cell) 
        pyobj = 
        cell.set_property('text', pyobj)
        #"""
        #print [model.get_value(iter, 0),model.get_value(iter, 1),model.get_value(iter, 2)]
        #cell.set_property("markup",model.get_value(iter, 1))
        cell.set_property('weight-set', 0)
        str = model.get_value(iter, 1);
        
        if model.get_value(iter, 2)==0:
            cell.set_property('weight', pango.WEIGHT_ULTRABOLD)
            cell.set_property('weight-set', 1)
            #print model.get_value(iter, 0)
            #cell.set_property("markup","<u>got ya!</u>")
        cell.set_property("alignment",pango.ALIGN_RIGHT);
        if is_hebrew(str): 
            cell.set_property('xalign', 1)
            
        else:
            cell.set_property('xalign', 0)
        
        #treeviewcolumn.set_cell_data_func(cell_renderer, func, data=None)

class http_link(threading.Thread):
    
    canceled = False
    def __init__(self,url,stream):
        threading.Thread.__init__(self)
        self.url = url
        self.stream = stream 
    def run(self):  
        global cache 
        if not self.canceled:
            if not (cache.url_exists(self.url)):
                f = open_url(self.url)
                data = f.read();
                self.stream.write(data)
                cache.add(self.url,data)
            else:
                self.stream.write(cache.get(self.url));
            
    def cancel(self,stream,attr):
        self.canceled = True; 
class rss_thread(threading.Thread):
    stop = False;
    def __init__(self,db_resource,link,id):
        """"""
        threading.Thread.__init__(self)
        
        self.stop = False;
        self.id = id;
        self.link = link
    def stop_th(self):
        self.stop = True; 
    def run(self):
        #"""gets a link, and returns a feedparser instance"""
        db_lock.acquire()
        if self.stop==False: 
            con = sqlite.connect(os.path.expanduser("~/.pyreadr/readr.db"));
            con.row_factory = sqlite.Row 
            self.db = con;  
            feed_ins = feedparser.parse(self.link); 
            
             
            check_feed_title = self.db.cursor();
            check_feed_title.execute("SELECT * FROM channels WHERE id="+self.id.__str__()+" AND name='updating...'");
            bool =0;
            for a in check_feed_title:
                bool=1;
            check_feed_title.close(); 
            update_name = self.db.cursor();
            update_name.execute("UPDATE channels SET name='"+feed_ins['feed']['title'][0:255]+"' WHERE id="+self.id.__str__());
            update_name.close();  
            i=0
            con = self.db 
            cur = con.cursor()
            items = range(len(feed_ins['entries']));
            items.reverse();
            refresh = False;
            for i in items:
                post = feed_ins['entries'][i];
                if 'updated' in post:
                    hash = md5.new(post.updated).hexdigest()
                    updated = post.updated
                else:
                    hash =""
                    updated = ""
                """check if exists"""
                channels = self.db.cursor();
                channels.execute("select * from posts WHERE channel_id='"+str(self.id)+"' AND link='"+post.link+"' AND updated_hash='"+hash+"'");
                j=0
                #print channels.lastrowid
                for row in channels:
                    j = j+1;
                    exit
                    #print row;  
                channels.close()  
                """if not exists, add it"""
                if j==0:
                    
                    if "summary_detail" in  post:
                        if "value" in  post.summary_detail:
                            summery_detial = post.summary_detail['value'];
                    else:
                        summery_detial = ""
                    
                    cur.execute ("INSERT INTO posts(read,channel_id,title,text,updated,updated_hash,link) VALUES(0,?,?,?,?,?,?)",
                         (str(self.id),post.title,summery_detial,updated,hash,post.link))             
                    refresh = True;
                    
                    gobject.timeout_add(500, readr.new_post_event_check )
      
                con.commit();
                cur.close();
                
            con.close(); 
            if refresh:
                gobject.timeout_add(500, readr.refresh_gui_treeviews)
        db_lock.release()
        return 0;
    def __del__(self):
        """"""
 
class AddRss:
    """ add_rss dialog class """
    def __init__(self,gladefile,db):
        self.gladefile = gladefile;
        self.db = db;"""TODO! no no no! need to open new db!"""
    def test(self,a,b):
        print "ok";
    def run(self,dest_function,feedurl=None):
        """opens the dialog"""
        self.wTree = gtk.glade.XML(self.gladefile,"add_rss");
        dic = {"on_add_rss_cancel" : self.close_add_rss, "test_event": self.test,\
               "on_add_rss_clicked" : self.add_rss, "on_add_rss_close" : dest_function }
        self.wTree.signal_autoconnect(dic);
        if feedurl !=None:
            url_entry = self.wTree.get_widget("rss_url");
            url_entry.set_text(feedurl);
        self.wTree.get_widget("errors").set_label("");
    def close_add_rss(self, widget):
        """closes the add_rss dialog"""
        self.wTree.get_widget("add_rss").destroy();
    
    def add_rss(self, widget):
        """checks the input from the dialog and adds RSS channel if correct"""
        self.wTree.get_widget("errors").set_label("");
        self.wTree.get_widget("errors").visible = True;
        ok=1
        """if  self.wTree.get_widget("rss_name").get_text().strip().__len__() == 0: #!FIXME !TODO fix no method get_text()
            self.raise_error("rss_name","The name is empty");
            ok = 0"""
        self.wTree.get_widget("rss_name").set_text(self.wTree.get_widget("rss_name").get_text().strip());
        if  self.wTree.get_widget("rss_url").get_text().strip().__len__() == 0: #!FIXME !TODO fix no method get_text()
            self.raise_error("rss_uri","The URL is empty");
            ok=0
        self.wTree.get_widget("rss_url").set_text(self.wTree.get_widget("rss_url").get_text().strip()) 
        if(ok):
            """if everything is ok just add to the DB and close the dialog"""
            con = self.db
            con.row_factory = sqlite.Row 
            cur = con.cursor()
            if self.wTree.get_widget("rss_name").get_text().strip().__len__() !=0 :
                rss_name = self.wTree.get_widget("rss_name").get_text();
            else:
                rss_name = "updating..";
            cur.execute("INSERT INTO channels (name,url) VALUES('"+rss_name+"','"+self.wTree.get_widget("rss_url").get_text()+"');") 
            """#print ("INSERT INTO channels (name,url) VALUES('"+self.wTree.get_widget("rss_name").get_text()+"','"+self.wTree.get_widget("rss_url").get_text()+"');") 
            """
            con.commit();
            cur.close();
            find_id = con.cursor()
            find_id.execute("SELECT * FROM channels ORDER BY id DESC LIMIT 1")#WHERE name='"+self.wTree.get_widget("rss_name").get_text()+"' AND url='"+self.wTree.get_widget("rss_url").get_text()+"'");
            for row in find_id:
                found = row
            self.wTree.get_widget("add_rss").destroy();
            th = rss_thread(self.db,found['url'],found['id']);
            th.start();
    def raise_error(self,widget,b):
        self.wTree.get_widget("errors").visible = True;
        if not self.wTree.get_widget("errors").get_label().__len__() ==0:
            self.wTree.get_widget("errors").set_label(self.wTree.get_widget("errors").get_label()+"\n");
        self.wTree.get_widget("errors").set_label(self.wTree.get_widget("errors").get_label()+" * "+b);
        self.wTree.get_widget("errors").show_all()
class conf:
    ballon = 0;#bool
    refresh_rate = 0; #int
    cache_clear_rate = 0; #int
    def __init__(self,db_resource):
        self.db = db_resource;
        
class options_dialog:
    def __init__(self,gladefile,db_resource):
        self.wTree = gtk.glade.XML(gladefile,"proporties_dialog");
        self.dialog = self.wTree.get_widget("proporties_dialog")
        self.db = db_resource
        self.wTree.signal_autoconnect({"dialog_close":self.close,"dialog_save":self.save})
        self.db2dialog();
    def run(self):
        self.dialog.run()
    def save(self,widget):
        """"""
    def db2dialog(self):
        """"""
    def close(self,widget):
        self.dialog.destroy();
class http_cache:
    cache = {}
    def __init__(self,cache_timeout):
        """cache_timeout - X seconds that a data should stay in cache"""
        self.cache_timeout = cache_timeout
    def add(self,url,data):
        self.cache[url]= (data,time.time());
    def get(self,url):
        return self.cache[url][0];
    def url_exists(self,url):
        if url in self.cache:
            return True;
        else:
            return False;
    def clean_all(self):
        del self.cache;
    def timed_clean(self):
        deletion = [];
        for url in self.cache:
            if time.time()-self.cache[url][1]>self.cache_timeout:
                deletion.append(url)#self.cache[url]
                 
        for url in deletion:
            del self.cache[url];
        return True;
class dbus_interface(dbus.service.Object):
    def __init__(self, bus_name, object_path='/org/freedesktop/pyreadrobject'):
        dbus.service.Object.__init__(self, bus_name, object_path)

    @dbus.service.method('org.freedesktop.pyreadriface')
    def add_feed(self,feedurl):
        print "adding...";
        readr.add_rss_dialog(feedurl);

if __name__ == "__main__":
    
    gtk.gdk.threads_enter()
    readr = pyReadr()
    cache = http_cache(3600);#360 - one hour.
    db_lock = thread.allocate_lock()
    session_bus = dbus.SessionBus()
    bus_name = dbus.service.BusName('org.freedesktop.pyreadr', bus=session_bus)
    object = dbus_interface(bus_name)
    gobject.timeout_add(1000, cache.timed_clean);
    gobject.timeout_add(60000, readr.refresh_all);
    gtk.main()
 
    gtk.gdk.threads_leave()
